import requests
from requests.packages import urllib3
import json
import dingding
from datetime import datetime
from threading import Timer

#监控


def watch():
    urllib3.disable_warnings()
    data = {'rp': 50, 'listed': 'Y'}
    req = requests.post('https://www.jisilu.cn/data/cbnew/cb_list/', data=data, verify=False)
    js = json.loads(req.text)
    rows = js['rows']
    print(js['total'])
    # print(js['rows'][0])
    # print(js['rows'][0]['cell']['bond_id'])
    # print(js['rows'][0]['cell']['bond_nm'])
    # for row in rows[:10]:
    message = ''
    is_send=False
    for row in rows:
        cell = row['cell']
        # if float(cell['price']) < 160.0 :
        if cell['sincrease_rt'] == '停牌' or float(cell['sincrease_rt'].replace('%', '')) > 9 or float(cell['sincrease_rt'].replace('%', '')) <-9:
            # message = 'code=%s name1=%s price1=%s per1=%s code2=%s name2=%s per2=%s yjl=%s synx=%s hsl=%s shsy=%s dblow=%s convert_dt=%s  force_redeem=%s' % (
            #     cell['bond_id'],
            #     cell['bond_nm'],
            #     cell['price'],
            #     cell['increase_rt'],
            #     cell['stock_id'],
            #     cell['stock_nm'],
            #     cell['sincrease_rt'], cell['premium_rt'], cell['year_left'], cell['turnover_rt'], cell['ytm_rt_tax'],
            #     cell['dblow'], cell['convert_dt'], cell['force_redeem'])
            message += cell['bond_nm'] +"="+cell['price']+ ";"+cell['increase_rt']+ ";"

            is_send=True
    if is_send:
        # pass
        print(message)
        # dingding.sendDingDing(message)
    printTime(10)
# 打印时间函数
def printTime(inc):
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    t = Timer(inc, watch)
    t.start()
# 5s
printTime(10)
# dingding.sendDingDing(message)
